/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.staf.NetworkReportGenerator;

import java.io.IOException;
import java.util.Map;
import java.util.List;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.Date;
import java.util.Set;
import java.util.LinkedList;
import java.util.LinkedHashMap;
import java.util.HashSet;
import org.xml.sax.SAXException;

/**
 *
 * @author aezhov
 */
public class ReportTableMaker {
    Iterator<Switch> swIt = null;
    LinkedHashMap<Switch, LinkedList> resultTable = new LinkedHashMap<Switch, LinkedList>();
    NmapXMLParser nmapParser = null;

    public class Record {
        public String portName;
        public String linkSpeed;
        public boolean iUp = true;
        public double bytesInAvg;
        public double bytesInMax;
        public double bytesInMin;
        public double bytesOutAvg;
        public double bytesOutMax;
        public double bytesOutMin;
        public double packetsInAvg;
        public double packetsInMax;
        public double packetsInMin;
        public double packetsOutAvg;
        public double packetsOutMax;
        public double packetsOutMin;
        public double errorsIn;
        public double errorsOut;
        public double discardsIn;
        public double discardsOut;
        public HashSet<String> macs = null;
        public HashSet<String> ips = null;
    }

    public ReportTableMaker(SwitchList swLst, Date date1, Date date2) throws
                                            org.jrobin.core.RrdException,
                                            java.io.IOException, 
                                            ConfigurationException,
                                            org.xml.sax.SAXException
    {
        this.swIt = swLst.buildSwitchlist();
        nmapParser = swLst.getNmapParser();
        
        while (swIt.hasNext()) {
            Switch sw = swIt.next();
            TreeSet<String> portDirs = sw.getPortDirectories();
            DeviceStatReader statReader = new DeviceStatReader(sw, portDirs, nmapParser, date1, date2);
            LinkedList<Record> swStat = new LinkedList<Record>();
            Iterator<String> it = portDirs.iterator();
            while (it.hasNext()) {
                String portDir = it.next();
                Record rec = new Record();
                rec.portName = portDir;
                rec.bytesInAvg = statReader.getIfInOctets(portDir, "AVERAGE");
                rec.bytesInMax = statReader.getIfInOctets(portDir, "MAX");
                rec.bytesInMin = statReader.getIfInOctets(portDir, "MIN");
                rec.bytesOutAvg = statReader.getIfOutOctets(portDir, "AVERAGE");
                rec.bytesOutMax = statReader.getIfOutOctets(portDir, "MAX");
                rec.bytesOutMin = statReader.getIfOutOctets(portDir, "MIN");
                rec.packetsInAvg = statReader.getIfInPackets(portDir, "AVERAGE");
                rec.packetsInMax = statReader.getIfInPackets(portDir, "MAX");
                rec.packetsInMin = statReader.getIfInPackets(portDir, "MIN");
                rec.packetsOutAvg = statReader.getIfOutPackets(portDir, "AVERAGE");
                rec.packetsOutMax = statReader.getIfOutPackets(portDir, "MAX");
                rec.packetsOutMin = statReader.getIfOutPackets(portDir, "MIN");
                rec.errorsIn = statReader.getIfInErrors(portDir, "MAX");
                rec.errorsOut = statReader.getIfOutErrors(portDir, "MAX");
                rec.discardsIn = statReader.getInDiscards(portDir);
                rec.discardsOut = statReader.getOutDiscards(portDir);
                rec.linkSpeed = statReader.getLinkSpeed(portDir);
                rec.macs = statReader.getMacMap(portDir);
                rec.ips = statReader.getIpMap(portDir);
                
                // Info created
                swStat.add(rec);
            }
            resultTable.put(sw, swStat);
        }

        // Calculating cross-switch relations
        new CrossSwitchCalculator(swLst.swList, "C8:0A:A9:DA:FC:87");
    }

    public LinkedHashMap<Switch, LinkedList> getTable() {
        return resultTable;
    }
}
